//
//  AnotherViewController.m
//  UI 作业 - 豆瓣电影
//
//  Created by hesiyao on 15/12/27.
//  Copyright © 2015年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "AnotherViewController.h"
#import "MovieModel.h"
#import "MovieCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "MovieDetailViewController.h"
@interface AnotherViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *movieArr;
@end

@implementation AnotherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // 初始化
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(94, 15, 49, 15);
    layout.itemSize = CGSizeMake((375 - 60) / 3, 180);
    layout.minimumLineSpacing = 25;
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    [self.view addSubview:_collectionView];
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [_collectionView registerClass:[MovieCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self DataHandler];
}
/// 数据处理
-(void)DataHandler
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    NSArray *arr = [NSMutableArray arrayWithContentsOfFile:path];
    //    self.MovieDic = [NSMutableDictionary dictionary];
    self.movieArr = [MovieModel modelHandlerCreate:arr];
}



-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _movieArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MovieCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor yellowColor];
    MovieModel *m = _movieArr[indexPath.item];
    cell.titleLabel.text = m.title;
    [cell.titleImg sd_setImageWithURL:[NSURL URLWithString:m.image]];
    cell.ratingLabel.text = m.rating;
    // 判断多少个星星了奥!!
    NSString *rating = m.rating;
//    CGFloat rate = rating.floatValue;
    NSLog(@"%@", rating);
    NSLog(@"%f", rating.floatValue);
    cell.firstImg.image = [UIImage imageNamed:@"ic_star_half@2x"];
    cell.secondImg.image = [UIImage imageNamed:@"ic_star_gray@2x"];
    cell.thirdImg.image = [UIImage imageNamed:@"ic_star_gray@2x"];
    cell.FourImg.image = [UIImage imageNamed:@"ic_star_gray@2x"];
    cell.FiveImg.image = [UIImage imageNamed:@"ic_star_gray@2x"];
    if (rating.floatValue <= 2.0 && rating.floatValue > 0) {
        cell.firstImg.image = [UIImage imageNamed:@"ic_star_half@2x"];
    }
    if (rating.floatValue > 2.0 && rating.floatValue < 4.0) {
        if (rating.floatValue < 3.0) {
            cell.firstImg.image = [UIImage imageNamed:@"ic_star_red@2x"];
            
        }if (rating.floatValue >= 3.0 ) {
            cell.firstImg.image = [UIImage imageNamed:@"ic_star_red@2x"];
            cell.secondImg.image = [UIImage imageNamed:@"ic_star_half@2x"];
        }
    }
    if (rating.floatValue >= 4.0 && rating.floatValue < 6.0) {
        if (rating.floatValue < 5.0) {
            cell.firstImg.image = [UIImage imageNamed:@"ic_star_red@2x"];
            cell.secondImg.image = [UIImage imageNamed:@"ic_star_red@2x"];
        }if (rating.floatValue >= 5.0 ) {
            cell.firstImg.image = [UIImage imageNamed:@"ic_star_red@2x"];
             cell.secondImg.image = [UIImage imageNamed:@"ic_star_red@2x"];
            cell.thirdImg.image = [UIImage imageNamed:@"ic_star_half@2x"];
        }
            }
    if (rating.floatValue >= 6.0 && rating.floatValue < 8.0) {
        if (rating.floatValue < 7.0) {
            cell.firstImg.image = [UIImage imageNamed:@"ic_star_red@2x"];
            cell.secondImg.image = [UIImage imageNamed:@"ic_star_red@2x"];
            cell.thirdImg.image = [UIImage imageNamed:@"ic_star_red@2x"];
        }if (rating.floatValue >= 7.0 ) {
            cell.firstImg.image = [UIImage imageNamed:@"ic_star_red@2x"];
            cell.secondImg.image = [UIImage imageNamed:@"ic_star_red@2x"];
            cell.thirdImg.image = [UIImage imageNamed:@"ic_star_red@2x"];
            cell.FourImg.image = [UIImage imageNamed:@"ic_star_half@2x"];
        }

            }
    
    if (rating.floatValue >= 8.0 && rating.floatValue < 10.0) {
        if (rating.floatValue < 9.0) {
            cell.firstImg.image = [UIImage imageNamed:@"ic_star_red@2x"];
            cell.secondImg.image = [UIImage imageNamed:@"ic_star_red@2x"];
            cell.thirdImg.image = [UIImage imageNamed:@"ic_star_red@2x"];
            cell.FourImg.image = [UIImage imageNamed:@"ic_star_red@2x"];
        }if (rating.floatValue >= 9.0 ) {
            cell.firstImg.image = [UIImage imageNamed:@"ic_star_red@2x"];
            cell.secondImg.image = [UIImage imageNamed:@"ic_star_red@2x"];
            cell.thirdImg.image = [UIImage imageNamed:@"ic_star_red@2x"];
            cell.FourImg.image = [UIImage imageNamed:@"ic_star_red@2x"];
            cell.FiveImg.image = [UIImage imageNamed:@"ic_star_half"];
            
        }
    }
    
    return cell;
}

/// 点击cell实现的方法

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"hehe");
    MovieDetailViewController *movieDetailVC = [[MovieDetailViewController alloc] init];
//    movieDetailVC.moedel = _movieArr[indexPath.item];
    [self.navigationController pushViewController:movieDetailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
